package cn.itsource.pet.service.impl;

import cn.itsource.basic.service.IBaiduAiAuditService;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.pet.domain.Pet;
import cn.itsource.pet.domain.PetDetail;
import cn.itsource.pet.domain.PetOnlineAuditLog;
import cn.itsource.pet.mapper.PetDetailMapper;
import cn.itsource.pet.mapper.PetMapper;
import cn.itsource.pet.mapper.PetOnlineAuditLogMapper;
import cn.itsource.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private IBaiduAiAuditService baiduAiAuditService;

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Transactional
    @Override
    public void add(Pet pet){
        super.add(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            //将宠物表的id设置到宠物详情中的petid
            petDetail.setPetId(pet.getId());
            //添加宠物详情
            petDetailMapper.add(petDetail);
        }
    }

    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        //获取宠物详情
        PetDetail petDetail = pet.getPetDetail();
        if (petDetail != null){
            //修改宠物详情
            petDetailMapper.update(petDetail);
        }
    }

    @Transactional
    @Override
    public void delete(Long id) {
        super.delete(id);
        //根据宠物详情的id进行删除
        petDetailMapper.deleteByPetDetailId(id);
    }

    @Transactional
    @Override
    public void batchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            //查询出一个pet对象
            Pet pet = petMapper.findById(id);
            //1.上架审核【内容审核，图像审核】 - 每一个宠物
            //获取宠物图片与文本并进行审核
            Boolean imageAudit = baiduAiAuditService.imageAudit(pet.getResources());
            Boolean textAudit = baiduAiAuditService.textAudit(pet.getName());
            //获取宠物审核日志
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            //设置宠物日志中的petId
            log.setPetId(id);
            //如果图片审核和文本审核都通过
            if (imageAudit && textAudit){
                //改状态
                petMapper.onsale(id,new Date());
                //审核成功：状态改为1 ，要添加审核日志 - 审核成功为1
                log.setState(1);
                log.setNote("审核成功");
                //将审核信息添加到审核表里
                petOnlineAuditLogMapper.add(log);
            }else {
                //2.审核失败：状态不改 ，要添加审核日志 - 审核失败为0
                log.setState(0);
                log.setNote("审核失败");
                petOnlineAuditLogMapper.add(log);
                return new AjaxResult().setSuccess(false).setMessage("上架失败");
            }
        }
        return new AjaxResult();
    }

    @Override
    public AjaxResult offsale(List<Long> ids) {
        //直接改,写sql
        //Mybatis多参数操作：1.Map 2.对象 3.索引下标#{0} 4.注解
        petMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
